package com.neusoft.neuiotms.dcom.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.KnowledgeTypeDTO;
import com.neusoft.neuiotms.dcom.model.KnowledgeType;
import com.neusoft.neuiotms.dcom.repository.KnowledgeTypeRepository;

/**
 * 
 * @author Liukq
 *
 */
@Component
public class KnowledgeTypeConverter extends AbstractConverter<KnowledgeType,KnowledgeTypeDTO> {

    @Autowired
    private KnowledgeTypeRepository knowledgeTypeRepository;
    
    
    @Override
    public KnowledgeType toModel(final KnowledgeTypeDTO dto) {
        // TODO Auto-generated method stub
        KnowledgeType model;
        if(dto.isNew()) {
            model = new KnowledgeType();
        }else {
            model = this.knowledgeTypeRepository.getOne(dto.getId());
        }
        model.setCode(dto.getCode());
        model.setName(dto.getName());
        model.setSort(dto.getSort());
        if((dto.getParentId() != null) && (dto.getParentId() != -1)) {
            model.setParent(this.knowledgeTypeRepository.getOne(dto.getParentId()));
        }
        if(dto.getParentCode() != null) {
            model.setParent(this.knowledgeTypeRepository.findByCode(dto.getParentCode()));
        }
        return model;
    }

    @Override
    public KnowledgeTypeDTO toDTO(final KnowledgeType model, final boolean forListView) {
        // TODO Auto-generated method stub
        final KnowledgeTypeDTO dto = new KnowledgeTypeDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setCode(model.getCode());
        dto.setSort(model.getSort());
        if(model.getParent() != null) {
            dto.setParentId(model.getParent().getId());
            dto.setParentName(model.getParent().getName());
            dto.setParentCode(model.getParent().getCode());
        }
        return dto;
    }


}
